function _ajax_exception_alert_(data) {
    if (data.showStackTrace) {
        var html = ''
            // + '<div style="text-align: center; margin: 10px; font-size: 15px; white-space: nowrap; overflow: hidden;">' + data.msg + '</div>'
            + '<div style="height: 150px; text-align: left; border: #b6bac9 solid 1px; border-radius: 5px; margin: 5px 20px; overflow: auto; font-size: 10px; background-color: #f5f5f5;">'
            + '<pre style="overflow: initial; font-size: 10px; margin: 0; border: 0;">' + data.msg + '</pre>'
            + '</div>';

        layer.open({
            type: '1',
            title: '提示信息',
            btn: '确定',
            content: html,
            area: ['400px','auto']
        });
    } else {
        layer.alert(data.msg);
    }
}

function AjaxRequest(url, data, callback, failedCallback) {
    this.url = url;
    this.data = data;
    this.callback = callback;
    this._async = false;
    this.contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
    this.failedCallback = failedCallback;
}

AjaxRequest.prototype.async = function () {
    this._async = true;
    return this;
};

AjaxRequest.prototype.json = function () {
    this.contentType = 'application/json;charset=UTF-8';
    if (typeof this.data != 'string') {
        this.data = JSON.stringify(this.data);
    }
    return this;
};

AjaxRequest.prototype.execute = function () {
    var result = null;
    var callback = this.callback;
    var failedCallback = this.failedCallback;
    if (this._async) {
        $.ajax({
            type: 'post',
            async: true,
            url: this.url,
            data: this.data,
            dataType: 'json',
            contentType: this.contentType,
            success: function (data) {
                if (data.type && data.type === 'error') {
                    _ajax_exception_alert_(data);
                    if(failedCallback) {
                        failedCallback(data.msg);
                    }
                } else {
                    result = data;
                    if (callback) {
                        callback(result);
                    }
                }
            },
            error: function (XMLHttpRequest) {
                layer.alert('请求失败,HTTP' + XMLHttpRequest.status);
            }
        });
    } else {
        $.ajax({
            type: 'post',
            async: false,
            url: this.url,
            data: this.data,
            dataType: 'json',
            contentType: this.contentType,
            success: function (data) {
                if (data.type && data.type === 'error') {
                    _ajax_exception_alert_(data);
                    if(failedCallback) {
                        failedCallback(data.msg);
                    }
                } else {
                    result = data;
                    if (callback) {
                        callback(result);
                    }
                }
            },
            error: function (XMLHttpRequest) {
                layer.alert('请求失败,HTTP' + XMLHttpRequest.status);
            }
        });
        return result;
    }
};


function AjaxForm(dom, url, data, callback) {
    this.dom = dom;
    this.url = url;
    this.data = data;
    this.callback = callback;
}

AjaxForm.prototype.execute = function () {
    var result = false;
    var callback = this.callback;
    $(this.dom).ajaxForm({
        type: 'post',
        url: this.url,
        data: this.data,
        dataType: 'json',
        success: function (data) {
            debugger
            if (data.type && data.type === 'error') {
                _ajax_exception_alert_(data);
            } else {
                result = data;
                if (callback) {
                    callback(result);
                }
            }
        },
        error: function (XMLHttpRequest) {
            layer.alert('请求失败,HTTP' + XMLHttpRequest.status);
        }
    });
};

function formCallback(data) {
    if (!data) {
        return false;
    }
    layer.confirm(data, function (index) {
        layer.close(index);
        var index = parent.layer.getFrameIndex(window.name);
        if (parent.$('#layer_refresh').length > 0) {
            parent.$('#layer_refresh').val('true');
        }
        parent.layer.close(index);
    });
}

// 获取当前url中的参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }

    return null;
}
//判断浏览器
function myBrowser() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    if (!(userAgent.indexOf("Chrome") != -1)) {
        alert("请使用谷歌Chrome浏览器打开页面");
    }
}
$(function () {
    myBrowser();
})


// 米-》千米
function m2km(distance) {
    distance = Number(distance);
    distance = distance / 1000;
    return distance.toFixed(2);
}